package com.gdk.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.gdk.auth.application.convert.AuthUserDTOConverter;
import com.gdk.auth.application.dto.AuthUserDTO;
import com.gdk.auth.common.entity.Result;
import com.gdk.auth.domain.entity.AuthUserBO;
import com.gdk.auth.domain.service.AuthUserDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户controller
 */
@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * 注册用户
     */
    @PostMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("UserController.register.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getEmail()),"邮箱不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getPassword()),"密码不能为空");

            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.register(authUserBO));
        }catch (Exception e) {
            log.error("UserController.register.dto:{}", e.getMessage(),e);
            return Result.fail("注册用户失败");
        }
    }

    /**
     * 修改用户信息
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("UserController.update.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        }catch (Exception e) {
            log.error("UserController.update.dto:{}", e.getMessage(),e);
            return Result.fail("修改用户信息失败");
        }
    }

    /**
     * 删除用户
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("UserController.delete.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(),"用户id不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.delete(authUserBO));
        }catch (Exception e) {
            log.error("UserController.delete.dto:{}", e.getMessage(),e);
            return Result.fail("删除用户失败");
        }
    }

    /**
     * 用户启用/禁用
     */
    @PostMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("UserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(),"用户id不能为空");
            Preconditions.checkNotNull(authUserDTO.getStatus(),"用户状态不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        }catch (Exception e) {
            log.error("UserController.changeStatus.dto:{}", e.getMessage(),e);
            return Result.fail("删除用户失败");
        }
    }

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(String validCode) {
        try {
            log.info("UserController.doLogin.dto:{}", JSON.toJSONString(validCode));
            Preconditions.checkArgument(StringUtils.isNotBlank(validCode),"验证码不能为空");
            return Result.ok(authUserDomainService.doLogin(validCode));
        }catch (Exception e) {
            log.error("UserController.doLogin.dto:{}", e.getMessage(),e);
            return Result.fail("用户登录失败");
        }
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
    
}
